<?php

include_once($cfgPaths['lib/dba'].'categoryTable.o.inc.php');

class dbaObjectTree{

  function dbaObjectTree (&$dba) {
    $this->dba = &$dba;
  }

  function checkChilds($id){
     $this->dba->query("select * from ".$this->dba->tableforwarder."_cmsObjCat where parent_id=$id;");
     if ($this->dba->num_rows()) return true;
  }

  function getArrayWhere (&$result,$whereclause){
    if ($whereclause){
      $whereclause = ' where '.$whereclause;
    }
    $check  = $this->dba->query("select * from ".$this->dba->tableforwarder."_cmsObjCat$whereclause order by name;");
    $result = $this->dba->fetch_rows_array();
    return $check;
  }

  function getArrayFieldsWhere (&$result,$fields,$whereclause){
    $count = count($fields);
    $fieldStr = '';
    for ($i = 0; $i < $count; $i++){
      if ($i == $count-1){
        $fieldStr .= $fields[$i];
      } else {
        $fieldStr .= $fields[$i].",";
      }
    }
    if ($whereclause){
      $whereclause = ' where '.$whereclause;
    }
    $check  = $this->dba->query("select $fieldStr from ".$this->dba->tableforwarder."_cmsObjCat$whereclause order by name;");
    $result = $this->dba->fetch_rows_array();
    return $check;
  }

  function getArray (&$result){
    return $this->getArrayWhere($result,'');
  }

  function getArrayByParent (&$result,$id){
    return $this->getArrayWhere($result,'parent_id='.$id);
  }

 function getID($catname){
   $this->dba->query("select id from ".$this->dba->tableforwarder."_cmsObjCat where name=".$this->dba->quote($catname).";");
   $cat = $this->dba->fetch_row_Array();
   return $cat['id'];
 }

  function getByID (&$result,$id){
    $check  = $this->dba->query("select * from ".$this->dba->tableforwarder."_cmsObjCat where id=$id;");
    $result = $this->dba->fetch_row_array();
    return $check;
  }

  function getArrayFields (&$result,$fields){
     return $this->getArrayFieldsWhere($result,$fields,'');
  }

  function getObj (&$check){
    $this->getArray($result);
    return new categoryTable($result,'id','parent_id');
  }

  function getObjSortedNameParent ()
  {
    $check  = $this->dba->query("select * from ".$this->dba->tableforwarder."_cmsObjCat order by name,parent_id;");
    $result = $this->dba->fetch_rows_array();
    return new categoryTable($result,'id','parent_id');
  }

  function getObjFields (&$check,$fields){
    $this->getArrayFields($result,$fields);
    return new categoryTable($result,'id','parent_id');
  }

  function add ($parent_id,$name, $type){
    return $this->dba->query("insert into ".$this->dba->tableforwarder."_cmsObjCat (parent_id,name, allowed_type) values($parent_id,"
        .$this->dba->quote($name).",".$this->dba->quote($type).");");
  }

  function update ($id,$name, $type){
    return $this->dba->query("update ".$this->dba->tableforwarder."_cmsObjCat set name="
        .$this->dba->quote($name).", allowed_type='$type' where id = $id;");
  }

  function updateParent ($id,$parent){
    return $this->dba->query("update ".$this->dba->tableforwarder."_cmsObjCat set parent_id=$parent where id = $id;");
  }

  function updateCat ($id,$cat){
    return $this->dba->query("update ".$this->dba->tableforwarder."_cmsElement set cat_id=$cat where element_id = $id;");
  }

  function del ($id){
    return $this->dba->query("delete from ".$this->dba->tableforwarder."_cmsObjCat where id=$id;");
  }

}
?>
